<template>
  <div class="login-page">
    <el-card class="el-card">
      <div class="logo"></div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="chognzhi">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from "@/aip/login";

export default {
  name: "LoginIndex",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
          },
          {
            min: 5,
            max: 11,
            message: "长度在 5 到 11 个字符",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
          },
          {
            pattern: /^\w{5,11}$/,
            message: "请输入 5 到 10 位的密码",
          },
        ],
      },
    };
  },
  methods: {
    async onSubmit() {
      try {
        await this.$refs.form.validate();
        const res = await login(this.form);
        console.log(res);
        this.$store.commit("user/setToken", res.data.token);
        this.$router.push("/");
      } catch (e) {
        console.log(e);
      }
    },
    async chognzhi() {
      await this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login-page {
  min-height: 100vh;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-card {
    width: 450px;
    height: 300px;
    position: relative;
    overflow: visible;

    .logo {
      overflow: visible;
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 3px solid #ccc;
      background: url("http://weldon.net.cn/back/img/logo2.fc048282.png")
        no-repeat center/cover #eeeeee;
      box-shadow: 0 0 20px #ccc;
      position: absolute;
      z-index: 3;
      top: -25%;
      left: 35%;
    }

    .el-form {
      padding-top: 70px;
    }
  }
}
</style>
